﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using Microsoft.Phone.Controls;

using Coding4Fun.Cannon.Phone.Controls;

namespace Coding4Fun.Cannon.Phone
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
			InitializeComponent();
			LayoutRoot.Visibility = Visibility.Collapsed;
			SupportedOrientations = SupportedPageOrientation.Landscape;
        }


        UserControl _oldControl;
        UserControl _currentControl; 
        
        //TODO: Clean this code up later
        void navigationControl1_ButtonNavigationChanged(object sender, ButtonNavigationEventArgs e)
        {
			StopAccelerometer();
			
			switch (e.NewButton)
        	{
        		case ButtonNavigation.Status:
					SetCurrentView(statusView1, StatusViewDown);
					break;
                case ButtonNavigation.Drive:
					SetCurrentView(driveView1, DriveViewDown);
					driveView1.StartAccelerometer(); 
                    break;
                case ButtonNavigation.Aim:
                    SetCurrentView(aimView1, AimViewDown);
					aimView1.StartAccelerometer();
                    break;
                case ButtonNavigation.Fire:                   
                    SetCurrentView(fireView1, FireViewDown);
                    break;
                case ButtonNavigation.None:
                    break;                
                default:
                 break;
        	}
        }

		private void SetCurrentView(UserControl view, Storyboard animation)
		{
			if (IsCurrent(view))
				return;

			CheckMargin(view);

			SetCurrentControls(view);
			animation.Begin();
		}

    	private static void CheckMargin(FrameworkElement control)
    	{
			if (control == null)
				return;
            
			if (control.Margin.Top == 0)
                control.Margin = new Thickness(0, -480, 0, 0); 
    	}

    	private void SetCurrentControls(UserControl newControl)
        {
            _oldControl = _currentControl;
            Canvas.SetZIndex(_oldControl, 0); 
            newControl.Visibility = Visibility.Visible;
            _currentControl = newControl;
            Canvas.SetZIndex(newControl, 1); 
        }

        private bool IsCurrent(UserControl control)
        {
        	return control.GetType() == _currentControl.GetType();
        }

    	private void StopAccelerometer()
        {
            //Stop
            App.Accelerometer.Stop(); 

            //unsubscribe from events
            driveView1.StopAccelerometer();
            aimView1.StopAccelerometer();

        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
			LayoutRoot.Visibility = Visibility.Visible;

            AddStoryboardEventHandlers();
            
            _currentControl = statusView1;
            StatusViewDown.Begin(); 
        
        }

        private void AddStoryboardEventHandlers()
        {
            StatusViewDown.Completed += Animation_Completed;
            AimViewDown.Completed += Animation_Completed;
            DriveViewDown.Completed += Animation_Completed;
            StatusViewDown.Completed += Animation_Completed;
        }

        void Animation_Completed(object sender, EventArgs e)
        {
        	if (_oldControl == null)
				return;

        	SetZOrder(_oldControl, 0);
        	_oldControl.Margin = new Thickness(0, -480, 0, 0);
        	_oldControl.Visibility = Visibility.Collapsed;
        }

        private static void SetZOrder(UIElement control, int zOrder)
        {
			if (control == null)
				return;

        	Canvas.SetZIndex(control, zOrder);
        }
    }
}